
#include "history_policy.h"

static history_policy_manage_t  hp_info;

int history_policy_init(void)
{
    hp_info.len = 0;

    hp_info.e_head = (slist_t *)psram_malloc(sizeof(slist_t));
	if (hp_info.e_head == NULL) {
		l_d("e_head null\n");
		return -1;
	}

    slist_init(hp_info.e_head);

    return 0;
}

int history_policy_destroy(void)
{
   	int ret = 0;
	
	slist_t* node;
	history_policy_t * obj = NULL;
    //******************************/
    if(!hp_info.e_head){
        return -1;
    }
    //******************************/
	slist_for_each(node, (hp_info.e_head))
	{
		obj = slist_entry(node,history_policy_t,e_tail);
		if(obj)
        {
            slist_remove(hp_info.e_head, &(obj->e_tail));
            if(obj->e_attr.buff){
                psram_free(obj->e_attr.buff);
                obj->e_attr.buff = NULL;
            }
            psram_free(obj);
            obj = NULL;
            hp_info.len--;
		}
	}
    //******************************/
    if(hp_info.e_head){
        psram_free(hp_info.e_head);
        hp_info.e_head = NULL;
    }
    //******************************/

    return 0;
}

int history_policy_set_last(int index)
{
    hp_info.last_index = index;
    return 0;
}

int history_policy_get_last(void)
{
    return hp_info.last_index;
}


int history_policy_add(int y,int m,int d, char *txt)
{
    int len = 0;
    history_policy_t * obj = NULL;

    obj = (history_policy_t *)psram_malloc(sizeof(history_policy_t));
    if(obj == NULL){
        l_d("obj err\n");
        return -1;
    }
    memset(obj, 0, sizeof(history_policy_t));
    slist_init(&(obj->e_tail));
    //**************************************************
    obj->e_attr.t_year  = y;
    obj->e_attr.t_month = m;
    obj->e_attr.t_day   = d;

    len = strlen(txt);

    obj->e_attr.buff    = (char *)psram_malloc(len);
    if(obj->e_attr.buff == NULL){
        l_d("buff err\n");
        psram_free(obj);
        obj = NULL;
        return -1;
    }

    memset(obj->e_attr.buff, 0, len);
    snprintf(obj->e_attr.buff,1024,"%s",txt);
    //**************************************************
    hp_info.len++;
    slist_add_tail(hp_info.e_head,&(obj->e_tail));
    //**************************************************

    return 0;
}


int history_policy_del(int y,int m,int d)
{
	int ret = 0;
	
	slist_t* node;
	history_policy_t * obj = NULL;

    if(!hp_info.e_head){
        return -1;
    }

	if(slist_isempty(hp_info.e_head)){
		return -1;
	}

	slist_for_each(node, (hp_info.e_head))
	{
		obj = slist_entry(node, history_policy_t, e_tail);
		if(obj)
        {
            if((obj->e_attr.t_year==y)&&(obj->e_attr.t_month==m)&&(obj->e_attr.t_day==d))
		    {
                l_d("del ymd=%d:%d:%d\n",obj->e_attr.t_year,obj->e_attr.t_month,obj->e_attr.t_day);
                ret = 1;
                break;
            }	
		}
	}

	if(ret){
		slist_remove(hp_info.e_head, &(obj->e_tail));
		hp_info.len--;

        if(obj){
            if(obj->e_attr.buff){
                psram_free(obj->e_attr.buff);
                obj->e_attr.buff = NULL;
            }
            psram_free(obj);
            obj = NULL;
        }
	}

	return 0;
}

int history_policy_trav(void)
{
	slist_t* node;
    history_policy_t * obj = NULL;

    if(!hp_info.e_head){
        return -1;
    }

	if(slist_isempty(hp_info.e_head)){
		l_d("list empty.\n");
		return -1;
	}

	slist_for_each(node,(hp_info.e_head))
	{
		obj = slist_entry(node, history_policy_t,e_tail);
		if(obj){
			#if 1
			l_d("ymd=%d:%d:%d:%s\n",obj->e_attr.t_year,obj->e_attr.t_month,obj->e_attr.t_day,obj->e_attr.buff);
			#endif
		}
	}

	return 0;
}

int history_policy_get_len(void)
{
    l_d("len=%d\n",hp_info.len);

    return hp_info.len;
}

history_policy_t * history_policy_get_by_index(int index)
{
    int len = 0;
	slist_t* node;
    history_policy_t * obj = NULL;
    
    if(!hp_info.e_head){
        return -1;
    }

	if(slist_isempty(hp_info.e_head)){
		l_d("list empty.\n");
		return NULL;
	}

	slist_for_each(node,(hp_info.e_head))
	{
		obj = slist_entry(node, history_policy_t,e_tail);
		if(obj){
            if(len == index)
            {
                #if 1
                l_d("find index=%d:%s\n",index,obj->e_attr.buff);
                #endif
                return obj;
            }
            len++;  
		}
	}

	return NULL;
}


